package com.taichuan.keysupport.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.BaseAdapter;

import com.taichuan.keysupport.Key;

/**
 * Created by OISNY on 2016/8/19.
 */
public class KS12ListView extends TcKeyListView {

    public KS12ListView(Context context) {
        super(context);
        init();
    }

    public KS12ListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public KS12ListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    /**
     * 定义一个 太川按键版 【12键】 设备专用的 ListView 按键事件监听回调对象
     */
    private final View.OnKeyListener tcKeyListener = new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            Log.i(TAG, "onKey: 接收到按键事件：  " + keyCode);
            BaseAdapter adapter = (BaseAdapter) getAdapter();
            int size = adapter.getCount();
            if (Key.Convert.KEYCODE_2 == keyCode) {
                setNotouchMode = true;
                KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN,
                        KeyEvent.KEYCODE_DPAD_UP);
                dispatchKeyEvent(keyEvent);
                return true;
            } else if (Key.Convert.KEYCODE_8 == keyCode) {
                setNotouchMode = true;
                KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN,
                        KeyEvent.KEYCODE_DPAD_DOWN);
                dispatchKeyEvent(keyEvent);
                return true;
            } else if (Key.Convert.KEYCODE_JING == keyCode) {
                int pos = getSelectedItemPosition();
                performItemClick(KS12ListView.this, pos, pos);
                return true;
            }
            return false;
        }
    };

    @Override
    protected View.OnKeyListener setTcKeyListener() {
        return tcKeyListener;
    }
}